#include <iostream>
#include "main_form.h" // for value_list_view.h

Value_List_View::Value_List_View() :
menu_popup_(NULL)
{
	//create_popup_menu();
	//Fill popup menu:
	{
		Gtk::Menu::MenuList & menulist = access_item_popup_menu_.items();

		menulist.push_back( Gtk::Menu_Helpers::MenuElem("修改(_E)",
			sigc::mem_fun(*this, &Value_List_View::on_modify) ) );
		menulist.push_back( Gtk::Menu_Helpers::MenuElem("修改二进制数据(_B)",
			sigc::mem_fun(*this, &Value_List_View::on_create_key) ) );
		menulist.push_back( Gtk::Menu_Helpers::MenuElem("删除(_D)",
			sigc::mem_fun(*this, &Value_List_View::on_delete) ) );
		menulist.push_back( Gtk::Menu_Helpers::MenuElem("重命名(_R)",
			sigc::mem_fun(*this, &Value_List_View::on_create_key) ) );
		
		Gtk::MenuItem new_item_root("test item");
		
		//new_item_root->add_label("新建(_N)");
		
		Gtk::Menu* new_item_submenu = Gtk::manage(new Gtk::Menu());
		//new_item_root->set_submenu(*new_item_submenu);
		//new_item_submenu->set_title("ddd");
		
		
		new_item_submenu->items().push_back(
		Gtk::Menu_Helpers::MenuElem("新建项(_K)", sigc::mem_fun(*this, &Value_List_View::on_create_key)));
		new_item_root.set_submenu(*new_item_submenu);
		access_item_popup_menu_.append(new_item_root);
	}
	access_item_popup_menu_.accelerate(*this);
}

Value_List_View::~Value_List_View()
{
}

int
Value_List_View::create_popup_menu()
{
	if (popup_action_group_ ||
	popup_ui_manager_) return -1;
	popup_action_group_ = Gtk::ActionGroup::create();
	popup_action_group_->add(Gtk::Action::create("FileNewStandard",
              Gtk::Stock::NEW, "_New", "Create a new file"));
          /*sigc::mem_fun(*this, &Main_Form::on_menu_file_new_generic)0);*/

	popup_action_group_->add(Gtk::Action::create("FileNewFoo",
              Gtk::Stock::NEW, "New Foo", "Create a new foo"));
          /*sigc::mem_fun(*this, &Main_Form::on_menu_file_new_generic)0);*/

	popup_action_group_->add(Gtk::Action::create("FileNewGoo",
              Gtk::Stock::NEW, "_New Goo", "Create a new goo"));
          /*sigc::mem_fun(*this, &Main_Form::on_menu_file_new_generic)0);*/         

  popup_ui_manager_ = Gtk::UIManager::create();
  popup_ui_manager_->insert_action_group(popup_action_group_);

  //add_accel_group(popup_ui_manager_->get_accel_group());

  //Layout the actions in a menubar and toolbar:
  Glib::ustring right_popup_ui_info = 
        "<ui>"
        "  <popup name='PopupMenu'>"
        "<menu action='FileNewStandard'>"
        "    <menuitem action='FileNewStandard'/>"
        "    <menuitem action='FileNewFoo'/>"
        "    <menuitem action='FileNewGoo'/>"
        "      </menu>"
        "  </popup>"
        "</ui>";

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
    popup_ui_manager_->add_ui_from_string(right_popup_ui_info);
  }
  catch(const Glib::Error& ex)
  {
    std::cerr << "building menus failed: " <<  ex.what();
  }
  #else
  std::auto_ptr<Glib::Error> ex;
  popup_ui_manager_->add_ui_from_string(right_popup_ui_info, ex);
  if(ex.get())
  {
    std::cerr << "building menus failed: " <<  ex->what();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  //Get the menu:
	if (NULL == menu_popup_) {
	menu_popup_ = dynamic_cast<Gtk::Menu*>(
		popup_ui_manager_->get_widget("/PopupMenu")); 
	if(!menu_popup_)
		g_warning("menu not found");  	
  	}

	return 0;
}

bool 
Value_List_View::on_button_release_event(GdkEventButton* event)
{
	Gtk::TreeView::on_button_release_event(event);
	
  if( (event->type == GDK_BUTTON_RELEASE) && (event->button == 3) )
  {
   Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
  if(refSelection)
   {
   	 Gtk::TreeModel::iterator iter = refSelection->get_selected();
    	/*if(iter) {
   		printf("have a seletion\n");
      	access_item_popup_menu_.popup(event->button, event->time);
		} else {   	
			printf("haven't a seletion\n");
			new_item_popup_menu_.popup(event->button, event->time);	
		}*/
		access_item_popup_menu_.popup(event->button, event->time);

	} 

    return true; //It has been handled.
  }
  else
    return false;
}

void 
Value_List_View::on_create_key()
{
  std::cout << "A popup menu item was selected." << std::endl;

  /*Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
  if(refSelection)
  {
    Gtk::TreeModel::iterator iter = refSelection->get_selected();
    if(iter)
    {
      int id = (*iter)[m_Columns.m_col_id];
      std::cout << "  Selected ID=" << id << std::endl;
    }
  }*/
}

void	Value_List_View::on_modify()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection)
	{
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter)
		{
			//edit_selected_::emit(signal_impl* impl, typename type_trait<T_arg1>::take _A_a1, typename type_trait<T_arg2>::take _A_a2);
			Gtk::TreeModel::Path path(iter);
			edit_selected_(path, get_column(*path.begin()));
			//sigc::internal::
			//g_signal_emit_by_name(GTK_OBJECT(this->gobj()), "row_activated");
		}
	}
}

void	
Value_List_View::on_delete()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
	if(refSelection) {
		Gtk::TreeModel::iterator iter = refSelection->get_selected();
		if(iter) {
			delete_selected_(iter);
		}
	}
}

void
Value_List_View::on_rename()
{
}
